import { Body, Injectable } from '@nestjs/common';
import { BannerEntity } from '../entities';
import { Repository } from 'typeorm';
import { CreateBannerDto } from '../dto/create_banner.dto';
import { MediaService } from '@/modules/media/services';
import { InjectRepository } from '@nestjs/typeorm';

@Injectable()
export class BannerService {
  constructor(
    @InjectRepository(BannerEntity)
    private readonly bannerRepository: Repository<BannerEntity>,
    private mediaService: MediaService,
  ) {}
  async create(@Body() body: CreateBannerDto) {
    const media = await this.mediaService.detail(body.media);
    const banner = this.bannerRepository.create();
    banner.title = body.title;
    banner.desc = body.desc;
    banner.media = media;
    await banner.save();
    return banner;
  }
  async list() {
    return (
      await this.bannerRepository.find({
        relations: ['media'],
      })
    ).splice(0, 4);
  }

  async delete(id: string) {
    return await this.bannerRepository.delete(id);
  }
  async update(id: string, @Body() body: CreateBannerDto) {
    const media = await this.mediaService.detail(body.media);
    const banner = await this.bannerRepository.findOneByOrFail({
      id,
    });
    banner.title = body.title;
    banner.desc = body.desc;
    banner.media = media;
    await banner.save();
    return banner;
  }
}
// 用户登录 验证用户名密码 返回token token存入数据库 同时生成一个过期时间远大于token的refreshtoekn
// 其中用户和token的关系是一对多的关系 token和refreshtoken是一对一的关系

// 挟带token访问需要验证的api
